package com.base.themvx.pop

import android.content.Context
import android.view.LayoutInflater
import com.base.themvx.databinding.CommonLoadingViewBinding
import com.base.themvx.defNonSyncLazy
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView

/**
 ******************(^_^)***********************<br>
 * Author: fee<br>
 * <P>DESC:
 * 通用的加载数据 loading Pop
 * </p>
 * ******************(^_^)***********************
 */
class CommonLoadingCenterPop(context: Context) : CenterPopupView(context) {

    companion object Shower {

        fun show(
            context: Context,
            title: CharSequence,
            navigationBarColor: Int,
            isHasNavigationBar: Boolean,
            isCancelable: Boolean = false
        ): CommonLoadingCenterPop {
            val pop = CommonLoadingCenterPop(context)
            pop.mVB.tvTitle.text = title
            val builder = XPopup.Builder(context)
                .dismissOnBackPressed(isCancelable)
                .dismissOnTouchOutside(false)
                .hasNavigationBar(isHasNavigationBar)
            if (isHasNavigationBar) {
                builder.navigationBarColor(navigationBarColor)
            }
            builder.asCustom(pop)
                .show()
            return pop
        }
    }

    val mVB: CommonLoadingViewBinding by defNonSyncLazy {
        CommonLoadingViewBinding.inflate(
            LayoutInflater.from(context),
            centerPopupContainer,
            true
        )
    }

    override fun addInnerContent() {
//        super.addInnerContent()
        //here do nothing...
    }

    override fun onCreate() {
//        mVB.ivAnimView.setAnimation(R.raw.anim_loading)//todo....
//        mVB.ivAnimView.repeatCount = -1
//        mVB.ivAnimView.playAnimation()
    }

    fun showWithTitle(title: CharSequence) {
        mVB.tvTitle.text = title
//        if (!mVB.ivAnimView.isAnimating) {
//            mVB.ivAnimView.playAnimation()
//        }
        show()
    }

    override fun onDismiss() {
        super.onDismiss()
//        if (mVB.ivAnimView.isAnimating) {
//            mVB.ivAnimView.cancelAnimation()
//        }
    }
}